import { Component, OnInit } from '@angular/core';
import {Path} from "../../../../portal/admin/admin-breadcrumb/breadcrumb";
import {PageSubject} from "../../../../util/page-subject";
import {OneDialogService} from "../../../../widgets/one-dialog/one-dialog.service";
import {AccountsService} from "../accounts.service";
import {ResponseUtil} from "../../../../util/response-util";
import {MsgConstants} from "../../../../constants/constants";
import {ActivatedRoute, Router} from "@angular/router";
import {MsgResult} from "../../../../models/msg-result";

declare var $;

@Component({
  selector: 'app-account-set-themes',
  templateUrl: './account-set-themes.component.html',
  styleUrls:['./account-set-themes.component.css']
})
export class AccountSetThemesComponent implements OnInit {

  themes;
  themesId:string;
  constructor(
    private pageSubject: PageSubject,
    private router : Router,
    private route : ActivatedRoute,
    private dialogService: OneDialogService,
    private responseUtil: ResponseUtil,
    private accountsService: AccountsService
  ) { }

  ngOnInit() {
    this.pushPage();
    this.getThemes();
  }
  /**
   * 面包屑导航
   */
  private pushPage() {
    let path = new Path("权限系统", "");
    this.pageSubject.pushPage("账户管理", "设置主题", path);
  }
  /**
   * 获取主题
   */
  getThemes(){
    this.accountsService.getThemes().subscribe(
      result => this.responseUtil.success(result, this.processThemeResult),
      message => this.responseUtil.error(message)
    )
  }
  private processThemeResult = (result) =>{
    if (result && result.code == MsgConstants.MSG_CODE_OK) {
      this.themes = result.data;
      console.log(this.themes);
    }
    if (result && result.code != MsgConstants.MSG_CODE_OK) {
      this.dialogService.info(result.message);
    }
  }

  saveThemeId(theme,elem){
    this.themesId = theme.id;
    $(elem).find(".fa-square-o").addClass("fa-check-square-o").removeClass("fa-square-o");
    $(elem).siblings().find(".fa-check-square-o").removeClass("fa-check-square-o").addClass("fa-square-o");
    // let skin = theme.code;
    // if(theme.code == "default" || theme.code == "grape" || theme.code == "red" || theme.code == "green"  || theme.code == "blue"){
    //   $(elem).find(".fa-square-o").addClass("fa-check-square-o").removeClass("fa-square-o");
    //   $(elem).siblings().find(".fa-check-square-o").removeClass("fa-check-square-o").addClass("fa-square-o");
    //   skin = theme.code == "default"?"green":theme.code == "grape"?"purple":theme.code;
    // }else{
    //   skin = "green";
    //   this.dialogService.info("正在开发中");
    // }
  }
  /**
   * 设置主题
   */
  postAccountTheme(){
    let accountId = this.route.snapshot.params["accountId"];
    this.accountsService.postAccountThemes(accountId,this.themesId).subscribe(
      result => this.responseUtil.success(result, this.processResult),
      message => this.responseUtil.error(message)
    )
  }
  private processResult = (result: MsgResult) => {
    if (result && result.code == MsgConstants.MSG_CODE_OK) {
      this.dialogService.modalSuccess(result.message, () => this.cancelAccountSetThemes());
    }
    if (result && result.code != MsgConstants.MSG_CODE_OK) {
      this.dialogService.info(result.message);
    }
  }
  /**
   * 取消修改
   */
  cancelAccountSetThemes() {
    this.router.navigate(['..'], {relativeTo: this.route});
  }
}
